//
//  GYDPopPresentationManager.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/17.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

/// 自定转场展现
let GYDPresentationManagerDidPresented = "GYDPresentationManagerDidPresented"
/// 自定义转场消失
let GYDPresentationManagerDidDismissed = "GYDPresentationManagerDismissed"

class GYDPopPresentationManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    /// 定义标记记录当前是否是展现
    private var isPresent = false
    /// 消失回调
    var dissmissBlock : (() -> ())?
    /// 保存菜单的尺寸
    var presentFrame = CGRectZero
    
    
    // MARK: - UIViewControllerTransitioningDelegate
    // 该方法用于返回一个负责转场动画的对象
    // 可以在该对象中控制弹出视图的尺寸等
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let pc = GYDPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentFrame =  presentFrame
        return pc
    }
    
    // 该方法用于返回一个负责转场如何出现的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 该方法用于返回一个负责转场如何消失的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    
    // MARK: - UIPresentationController
    // 告诉系统展现和消失的动画时长
    // 暂时用不上
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.15
    }
    
    // 专门用于管理modal如何展现和消失的, 无论是展现还是消失都会调用该方法
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 0.判断当前是展现还是消失
        if isPresent {
            // 展现
            willPresentedController(transitionContext)
            
        }else {
            // 消失
            willDismissedController(transitionContext)
        }
    }
    
    /// 执行展现动画
    private func willPresentedController(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取需要弹出视图
        // 通过ToViewKey取出的就是toVC对应的view
        guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else {
            return
        }
        
        // 2.将需要弹出的视图添加到containerView上
        transitionContext.containerView()?.addSubview(toView)
        
        // 3.执行动画
        toView.alpha = 0.0
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            toView.alpha = 1.0
        }) { (_) -> Void in
            // 注意: 自定转场动画, 在执行完动画之后一定要告诉系统动画执行完毕了
            transitionContext.completeTransition(true)
        }
    }
    /// 执行消失动画
    private func willDismissedController(transitionContext: UIViewControllerContextTransitioning) {
        // 1.拿到需要消失的视图
        guard let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) else {
            return
        }
        // 2.执行动画让视图消失
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromView.alpha = 0.0
            }, completion: { (_) -> Void in
                if let tempDissmissBlcok = self.dissmissBlock {
                    tempDissmissBlcok()
                }
                // 注意: 自定转场动画, 在执行完动画之后一定要告诉系统动画执行完毕了
                transitionContext.completeTransition(true)
        })
    }
    deinit {
        print("pop模态跳转控制器代理消失")
    }
}
